package com.example.zr_rejectdemo;

import java.lang.reflect.Field;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	private Button mButton;
	private EditText mEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		setListener();
		
		Field field;
		try {
			field = mEditText.getClass()
					.getSuperclass()
					.getDeclaredField("mCursorDrawableRes");
            // 设置可访问
			field.setAccessible(true);
			// 设置mCursorDrawableRes的值
			field.set(mEditText, R.drawable.ic_launcher);
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}

	}

	private void initView() {
		mButton = (Button) findViewById(R.id.button);
		mEditText = (EditText)findViewById(R.id.tv);
	}

	private void setListener() {
		mButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog();
			}
		});
	}

	public void showDialog() {
		AlertDialog alertDialog = new AlertDialog.Builder(this)
				.setTitle(" abc ")
				.setMessage(" content ")
				.setIcon(R.drawable.ic_launcher)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {
							Field field = dialog.getClass().getSuperclass()
									.getDeclaredField("mShowing");
							field.setAccessible(true);
							// 将mShowing变量设为false，表示对话框已关闭
							field.set(dialog, false);
							dialog.dismiss();

						} catch (Exception e) {

						}
					}
				})
				.setNegativeButton("取消",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								try {
									Field field = dialog.getClass()
											.getSuperclass()
											.getDeclaredField("mShowing");
									field.setAccessible(true);
									// 将mShowing变量设为false，表示对话框已关闭
									field.set(dialog, false);
									dialog.dismiss();

								} catch (Exception e) {

								}
							}
						}).create();

//		try {
//
//			Field field = alertDialog.getClass().getDeclaredField("mAlert");
//			field.setAccessible(true);
//			// 获得mAlert变量的值
//			Object obj = field.get(alertDialog);
//			field = obj.getClass().getDeclaredField("mHandler");
//			field.setAccessible(true);
//			// 修改mHandler变量的值，使用新的ButtonHandler类
//			field.set(obj, new ButtonHandler(alertDialog));
//		} catch (Exception e) {
//		}
		// 显示对话框
		alertDialog.show();
	}
}